/**
 * 
 */
package com.ffcs.crmd.tsp.server.netty.processor;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.ffcs.crmd.tsp.api.dto.client.TspSliceGroup;
import com.ffcs.crmd.tsp.api.dto.netty.RemotingCommand;
import com.ffcs.crmd.tsp.api.dto.page.PageInfo;
import com.ffcs.crmd.tsp.api.dto.page.RetVo;
import com.ffcs.crmd.tsp.api.netty.INettyProcessor;
import com.ffcs.crmd.tsp.common.constant.LoggerName;
import com.ffcs.crmd.tsp.common.netty.ResponseCode;
import com.ffcs.crmd.tsp.core.util.BeanUtils;
import com.ffcs.crmd.tsp.core.util.XmlUtil;
import com.ffcs.crmd.tsp.server.core.DataBaseHandler;
import com.ffcs.crmd.tsp.server.core.RemotingHandler;
import com.ffcs.crmd.tsp.server.jdbc.entity.SliceGroup;

import io.netty.channel.ChannelHandlerContext;

/**
 * 功能说明:
 *
 * @author ZHONGFUHUA
 * 
 * @since 
 *
 */
public class QuerySliceGroupProcessor implements INettyProcessor {
    
    /**
     * 日志
     */
    private final static Logger LOG = LoggerFactory.getLogger(LoggerName.RemotingLoggerName);
    
    /**
     * 
     */
    private DataBaseHandler     dataBaseHandler;
    
    /**
     * 功能说明:
     * 
     * @param remotingController
     */
    public QuerySliceGroupProcessor(RemotingHandler remotingController, DataBaseHandler dataBaseHandler) {
        this.dataBaseHandler = dataBaseHandler;
    }
    
    /**
     * 功能说明:
     * 
     * @param ctx
     * @param request
     * @return
     */
    @Override
    public RemotingCommand processRequest(ChannelHandlerContext ctx, RemotingCommand request) {
        RemotingCommand response = RemotingCommand.createResponseCommand(ResponseCode.SUCCESS);
        String requestXml = new String(request.getBody());
        try {
            LOG.debug("SERVER-NETTY: one request to query slice group is received that body=[{}]", requestXml);
            TspSliceGroup tspSliceGroupVO = (TspSliceGroup) XmlUtil.fromXml(requestXml, TspSliceGroup.class);
            RetVo<TspSliceGroup> tspSliceGroupRetVo = new RetVo<TspSliceGroup>();
            Integer pageNumber = tspSliceGroupVO.getPageNumber();
            Integer pageSize = tspSliceGroupVO.getPageSize();
            
            SliceGroup sliceGroupParam = new SliceGroup();
            BeanUtils.applyIf(sliceGroupParam, tspSliceGroupVO);
            
            if (pageNumber != null && pageSize != null) {
                PageInfo<SliceGroup> sliceGroupPageInfo = dataBaseHandler.getServiceSupport().querySliceGroupPage(sliceGroupParam);
                PageInfo<TspSliceGroup> tspSliceGroupPageInfo = new PageInfo<TspSliceGroup>();
                List<TspSliceGroup> tspSliceGroupList = BeanUtils.copyList(sliceGroupPageInfo.getList(), TspSliceGroup.class);
                BeanUtils.applyIf(tspSliceGroupPageInfo, sliceGroupPageInfo);
                tspSliceGroupPageInfo.setList(tspSliceGroupList);
                tspSliceGroupRetVo.setPageInfo(tspSliceGroupPageInfo);
            } else if (tspSliceGroupVO.getId() != null) {
                TspSliceGroup tspSliceGroup = new TspSliceGroup();
                SliceGroup sliceGroup = dataBaseHandler.getServiceSupport().getSliceGroup(sliceGroupParam);
                BeanUtils.applyIf(tspSliceGroup, sliceGroup);
                tspSliceGroupRetVo.setObject(tspSliceGroup);
            } else {
                List<SliceGroup> sliceGroupList = dataBaseHandler.getServiceSupport().querySliceGroup(sliceGroupParam);
                List<TspSliceGroup> tspSliceGroupList = BeanUtils.copyList(sliceGroupList, TspSliceGroup.class);
                tspSliceGroupRetVo.setDataList(tspSliceGroupList);
            }
            response.setBody(XmlUtil.toXml(tspSliceGroupRetVo).getBytes());
        } catch (Exception e) {
            LOG.error("SERVER-NETTY: query slice group[{}] exception:", requestXml, e);
            String responseBody = String.format("query slice group:%s", e.getMessage());
            response.setCode(ResponseCode.FAIL);
            response.setBody(responseBody.getBytes());
        }
        return response;
    }
    
}
